<?php
//URL rewrite library;

    //Seperates the mod_rewrite url into php-usable args;
    /**
     * Takes a URL and produces a list of arguments from it.
     *
     * @param url to decode $url
     * @return array
     */
    function decode_mrw_url($url) {
        $conf = Config::Inst();
        $sc = explode('?',$url);
        $as = $sc[0];
        if ($as{0} == '/') {
            $as = substr($as,1); 
        }
        if (strstr($as,$conf->HTTPPath)) { 
            $as = substr($as,strlen($conf->HTTPPath));
        }
        if (substr($as,-1) == '/') {
            $as = substr($as,0,-1);
        } //Strip trailing/leading slashes and path if they exist
        $args = explode('/',$as);
        return $args;
    }
    

    function URL() {
        $conf = Config::Inst();
        $args = func_get_args();
        
        $Protocol = 'http' . (($conf->HTTPSecure == 1) ? 's' : '') . '://';
        $Server = $conf->HTTPRoot . (($conf->HTTPPort == '') ? '' : ':' . $conf->HTTPPort);
        $Path = $conf->HTTPPath;
        $Params = implode('/',$args);
        $URL = $Protocol . $Server . '/' . $Path . $Params;
        return $URL;
    }
    
    function PrintURL() {
        $conf = Config::Inst();
        $args = func_get_args();
        
        $Protocol = 'http' . (($conf->HTTPSecure == 1) ? 's' : '') . '://';
        $Server = $conf->HTTPRoot . (($conf->HTTPPort == '') ? '' : ':' . $conf->HTTPPort);
        $Path = $conf->HTTPPath;
        $Params = implode('/',$args);
        $URL = $Protocol . $Server . '/' . $Path . $Params;
        echo $URL;
    }
    
    //Produces a formatted URL. Necessary function in case mod_rewrite isn't available
    /**
     * Produces a URL 
     *
     * @return unknown
     */
    function format_url() {
        $conf = Config::Inst();
        $args = func_get_args();
        $out = 'http' . (($conf->HTTPSecure == 1) ? 's' : '');
        $out .= '://' . $conf->HTTPRoot . (($conf->HTTPPort == '') ? '' : ':' . $conf->HTTPPort);
        //switch ($conf->ModRewrite) { //Commenting this out because we'll never, ever not use mod-rewrite for this system - AS
            /*case 0:
                //Url should be of form http(s)://domain/path/index.php?args,args,args
                $out .= '/' . $conf->HTTPPath . 'index.php?' . implode('/',$args);
                break;*/
            //case 1:
                //Url should be of form http(s)://domain/path/args/args/args/
                $out .= '/' . $conf->HTTPPath . implode('/',$args);
                //break;
        //}
        return $out;
    }
    
    //For linking scripts, css etc
    function format_fixed_url() {
        $conf = Config::Inst();
        $args = func_get_args();
        $out = 'http' . (($conf->HTTPSecure == 1) ? 's' : '');
        $out .= '://' . $conf->HTTPRoot . (($conf->HTTPPort == '') ? '' : ':' . $conf->HTTPPort);
        //Url should be of form http(s)://domain/path/index.php?args,args,args
        $out .= '/' . $conf->HTTPPath . implode('/',$args);
        return $out;
    }
    
    function Trunc5($string) {
        $return = (strlen($string) > 4) ? substr($string,0,5) . '...' : $string;
        return $return;
    }
    
    function Trunc15($string) {
        $return = (strlen($string) > 14) ? substr($string,0,15) . '...' : $string;
        return $return;
    }
    
?>